package com.itheima.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component//推荐视频定时任务类
public class RecommentVideoJob {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    //@Scheduled(cron = "0/30 * * * * ?")
    @Scheduled(cron = "0 0 0/6 * * ?")//每6小时运行一次
    public void RecommentVideoUpdate() {
        System.out.println("推荐视频start");
        //从redis里查询前缀为QUANZI_VIDEO_RECOMMEND_的key
        Set<String> keys = stringRedisTemplate.keys("QUANZI_VIDEO_RECOMMEND_*");
        //判断非空遍历
        if (CollectionUtil.isNotEmpty(keys)) {
            for (String key : keys) {
                //替换掉前缀 得到userId
                String userId = key.replace("QUANZI_VIDEO_RECOMMEND_", "");
                //根据key查询values
                String values = stringRedisTemplate.opsForValue().get(key);
                //根据key删除redis里数据
                stringRedisTemplate.delete(key);
                //删除之前的推荐动态
                mongoTemplate.remove(new Query(Criteria.where("userId").is(Long.parseLong(userId))), RecommendVideo.class);
                //判断values非空
                if (StrUtil.isNotEmpty(values)) {
                    //使用，切割获得vids
                    String[] vids = values.split(",");
                    //遍历
                    for (String vid : vids) {
                        //调用mongo根据vid查询视频详情
                        Video video = mongoTemplate.findOne(new Query(Criteria.where("vid").is(Long.parseLong(vid))), Video.class);
                        //创建推荐video赋值
                        RecommendVideo recommendVideo = new RecommendVideo();
                        recommendVideo.setDate(System.currentTimeMillis());
                        recommendVideo.setScore(RandomUtil.randomDouble(70, 100));
                        recommendVideo.setUserId(Long.parseLong(userId));
                        recommendVideo.setVid(Long.parseLong(vid));
                        recommendVideo.setVideoId(video.getId());
                        //调用mongo保存
                        mongoTemplate.save(recommendVideo);
                    }
                    int i = 1;
                    //遍历redis里的视频缓存并删除
                    while (stringRedisTemplate.hasKey("tanhua:video_" + userId + "_" + i + "_10")) {
                        stringRedisTemplate.delete("tanhua:video_" + userId + "_" + i + "_10");
                        i++;
                    }
                }
            }
        }
        System.out.println("推荐视频end");

    }
}
